package com.ldgame.entities;

import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;

import com.ldgame.gfx.gfxelements.Sprite;
import com.ldgame.utils.Timer;

public class City {
	private Vector2f position;
	private Building [] buildings;
	private Team team;
	private float captureTimer = 0.0f;
	private boolean contested = false;
	private int sizeX;
	private int sizeY;
	private Sprite roadSprite = new Sprite(position,
										   256.0f,
										   256.0f,
										   new Vector2f(32.0f, 0.0f),
										   new Vector2f(64.0f, 64.0f),
										   new Color(1.0f, 1.0f, 1.0f, 1.0f));
	
	public City(Vector2f position, int sizeX, int sizeY, Team team, Vector2f camPosition) {
		this.position = position;
		this.team = team;
		this.sizeX = sizeX;
		this.sizeY = sizeY;
		buildings = new Building[sizeX * sizeY];
		roadSprite.setCamPosition(camPosition);
		
		for (int i = 0; i < sizeX * sizeY; ++i) {
			buildings[i] = new Building(team);
			buildings[i].setPosition(new Vector2f(position.x - 128f * (float)sizeX + (float)(i % sizeX) * 256.0f + 128.0f + 48.0f * ((float)Math.random() - 0.5f),
												  position.y - 128f * (float)sizeY + (float)(i / sizeX) * 256.0f + 98.0f + 48.0f * ((float)Math.random() - 0.5f)));
			buildings[i].getSprite().setColor(new Color(0.9f + 0.2f * (float)Math.random(), 0.9f + 0.2f * (float)Math.random(), 0.9f + 0.2f * (float)Math.random(), 1.0f));
			buildings[i].getSprite().setCamPosition(camPosition);
			buildings[i].getFlagSprite().setCamPosition(camPosition);
		}
	}
	
	public Vector2f getPosition() {
		return position;
	}
	
	public Team getTeam() {
		return team;
	}
	
	public void setTeam(Team team) {
		this.team = team;
	}
	
	public Building [] getBuildings() {
		return buildings;
	}
	
	public boolean isContested() {
		return contested;
	}

	public void setContested(boolean contested) {
		this.contested = contested;
	}

	public void sync(Entity [] entities) {
		captureTimer += Timer.getLoopTime();
		
		for (Building building : buildings) {
			for (Entity entity : entities) {
				building.handleCollisions(entity);
			}
		}
		
		if (captureTimer >= 10.0f) {
			captureTimer = 0.0f;
			
			int humanBuildings = 0;
			for (Building building : buildings) {
				int nearbyHumans = 0;
				int nearbyCreatures = 0;
				
				for (Entity entity : entities) {
					if (entity.isEnabled() && Vector2f.sub(entity.getPosition(), building.getPosition(), null).length() < 256.0f) {
						++nearbyCreatures;
						if (entity.getTeam() == Team.TINY_HUMAN) {
							++nearbyHumans;
						}
					}
					
					if (nearbyHumans > nearbyCreatures / 2) {
						building.setTeam(Team.TINY_HUMAN);
					} else if (nearbyHumans < nearbyCreatures / 2) {
						building.setTeam(Team.TINY_ALIEN);
					}
				}
				
				if (building.getTeam() == Team.TINY_HUMAN) {
					++humanBuildings;
				}
			}
			
			team = (humanBuildings == buildings.length) ? Team.TINY_HUMAN : Team.TINY_ALIEN;
			
			if (humanBuildings == 0 || humanBuildings == buildings.length) {
				contested = false;
			} else {
				contested = true;
			}
		}
	}
	
	public void handleCollisions(Entity entity) {
		for (Building building : buildings) {
			building.handleCollisions(entity);
		}
	}
	
	public void render() {
		for (int i = 0; i < buildings.length; ++i) {
			roadSprite.setPosition(new Vector2f(position.x - 128f * (float)sizeX + (float)(i % sizeX) * 256.0f + 128.0f,
					  							position.y - 128f * (float)sizeY + (float)(i / sizeX) * 256.0f + 128.0f));
			roadSprite.render();
		}
		
		for (Building building : buildings) {
			building.render();
		}
	}
}
